package com.comac.print.component.app.command.qryExe;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.print.component.app.dto.MasterPlateDTO;
import com.comac.print.component.app.gatewayimpl.database.dataobject.MasterPlateDO;
import com.comac.print.component.app.gatewayimpl.database.mapper.MasterPlateMapper;
import com.comac.print.component.app.page.PageResponse;
import com.comac.print.component.app.qry.MasterPlateListQry;
import com.comac.print.component.app.qry.VersionListQry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class MasterPlateUploadOssQryExe {

    @Autowired
    private MasterPlateMapper masterPlateMapper;


    public PageResponse<MasterPlateDTO> qryMasterPlateList(MasterPlateListQry masterPlateListQry) {
        if(masterPlateListQry.getPageNum()==0 || masterPlateListQry.getPageSize()==0){
            return  PageResponse.buildFailure("-1", "分页参数不为空！");
        }
        Page page = new Page(masterPlateListQry.getPageNum(), masterPlateListQry.getPageSize());
        log.info("---MasterPlateUploadOssQryExe.qryMasterPlateList---查询模板信息，入参：{}", JSON.toJSONString(masterPlateListQry));
        Page<MasterPlateDTO> masterPlatePage = masterPlateMapper.qryMasterPlateList(page,masterPlateListQry);
        log.info("---MasterPlateUploadOssQryExe.qryMasterPlateList---查询模板信息，出参：{}", JSON.toJSONString(masterPlatePage));
        List<MasterPlateDTO> masterPlateDTOList = masterPlatePage.getRecords();
        Page<MasterPlateDTO> pageData = new Page();
        List<MasterPlateDTO> result = putBasicInformation(masterPlateDTOList);
        pageData.setRecords(result);
        pageData.setCurrent(masterPlatePage.getCurrent());
        pageData.setTotal(masterPlatePage.getTotal());
        pageData.setSize(masterPlatePage.getSize());
        return PageResponse.successPage(pageData);
    }

    private List<MasterPlateDTO> putBasicInformation(List<MasterPlateDTO> masterPlateDTOList) {
        //设置FAI
        List<MasterPlateDTO> result = new ArrayList<>(masterPlateDTOList.size());
        for (MasterPlateDTO masterPlateDTO : masterPlateDTOList) {
            MasterPlateDTO masterPlateToDTO = new MasterPlateDTO();
            BeanUtils.copyProperties(masterPlateDTO, masterPlateToDTO);
            //枚举状态的转换
            //MpStatusEnum mpStatusEnumByCode = MpStatusEnum.getMpStatusEnumByCode(masterPlateDTO.getMpStatus());
            //masterPlateToDTO.setMpStatus(mpStatusEnumByCode.getDescription());
            result.add(masterPlateToDTO);
        }
        return result;
    }

    public List<MasterPlateDO> versionList(VersionListQry versionListQry) {
        QueryWrapper<MasterPlateDO> masterPlateDOQueryWrapper=new QueryWrapper<>();
        masterPlateDOQueryWrapper.eq("system_id", versionListQry.getSystemId());
        masterPlateDOQueryWrapper.eq("bussiness_id", versionListQry.getBussinessId());
        masterPlateDOQueryWrapper.eq("delete_flag", 0);
        masterPlateDOQueryWrapper.orderByDesc("operator_time");
        return masterPlateMapper.selectList(masterPlateDOQueryWrapper);
    }

}
